package by.semerikov.tests;

import static org.junit.Assert.*;

import java.util.List;

import org.junit.Test;

import by.semerikov.dao.Car;
import by.semerikov.dao.Person;
import by.semerikov.dao.factory.mysqlJdbc.CarDAO;
import by.semerikov.dao.factory.mysqlJdbc.PersonDAO;

public class CarDAOTest {

	
	
	@Test
	public void testCreate() {
		try{
			PersonDAO pd = new PersonDAO();
			Person testPerson = new Person(2, "Lasa", "Sada", "Admin", null, null);
			pd.create(testPerson);
			CarDAO cd = new CarDAO();
			Car testCar = new Car(2, "Ford", "FREE", testPerson);
			cd.create(testCar);
			Car test = cd.read(testCar).get(0);
			cd.delete(testCar);
			pd.delete(testPerson);
			assertTrue(test != null);
		}
		catch(Exception e)
		{
			
			e.printStackTrace();
			assertFalse(false);
		}
	}
	
	@Test
	public void testRead() {
		try{
			//PersonDAO pd = new PersonDAO();
			Person testPerson = new Person(2, "Lasa", "Sada", "Admin", null, null);
			
			CarDAO cd = new CarDAO();
			Car testCar = new Car(null, null, null, testPerson);
			List<Car> lc = cd.read(testCar);
			assertTrue( lc != null &&lc.size()!= 0);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			assertFalse(false);
		}
	}
	
	@Test
	public void testUpdate() {
		try{
			PersonDAO pd = new PersonDAO();
			Person testPerson = new Person(2, "Lasa", "Sada", "Admin", null, null);
			pd.create(testPerson);
			CarDAO cd = new CarDAO();
			Car testCar = new Car(3, "test", "test", testPerson);
			cd.create(testCar);
			List<Car> lc = cd.read(testCar);
			Car t = lc.get(0);
			t.setModel("BMW");
			cd.update(t);
			cd.delete(t);
			pd.delete(testPerson);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			assertFalse(false);
		}
	}

}
